// preload images
var ikz = 0; // initialize
var int = 0; // Internet Explorer Fix

jQuery( function() {
	jQuery( '.castshadow img' ).hide();
	jQuery( '.castshadow img' ).css( 'visibility', 'visible' );
	// early document ready bugfix for webkit & opera
	if( document.readyState != 'complete' ) {
		setTimeout( arguments.callee, 100 );
		return;
	} else {
		var int = setInterval( "show_page_images(ikz)", 100 );
		jQuery( 'img' ).wrapimages( '.castshadow' );
	}
} );

function show_page_images() {
	var imgs = jQuery( '.castshadow img' ).length;
	if( ikz >= imgs ) {
		clearInterval( int );
	}
	jQuery( '.castshadow img:hidden' ).eq( 0 ).fadeIn( 300 );
	ikz ++;
}

jQuery.fn.wrapimages = function( inContainer ) {
	// separate element and element container for more flexibility...container is optional
	if( inContainer == '' || inContainer == null || inContainer == 'undefined' ) inContainer = '.castshadow';
	var this_element = jQuery( this ).get( 0 ).tagName.toLowerCase();
	var referenced_as = inContainer + ' ' + this_element;
	
	// iterate through
	jQuery( referenced_as ).each( function() {
		
		if( jQuery( this ).width() < 80 ) return;
		
		// IE7 fix
		var this_image_width = parseInt( jQuery( this ).width() );
		var this_img_full_W  = this_image_width + 2;

		var curr_img_class = '';
		var wrap_with = '<span class="prodimgwrap" style="width:' + this_img_full_W + 'px;"></span>'; // wrap all with this
		var append_shadow = '<span class="imgshadow"><span class="shadowleft">&nbsp;</span><span class="shadowright">&nbsp;</span></span>'; // add shadow after img tag
		var add_rel_before = '<span class="prodtag">&nbsp;</span>'; // product taggr: new product, reduced, limited edition
		var is_wrapped = false;
		var wrap_me = jQuery( this );
		var wrap_parent = '';
		
		if( wrap_me.parent().get( 0 ).tagName.toLowerCase() == 'a' ) {
			is_wrapped = true;
			wrap_parent = jQuery( this ).parent();
		}
		
		var classes_arr = new Array();
		classes_arr = !wrap_me.attr( 'class' ) ? '' : wrap_me.attr( 'class' ).split( ' ' );
		
		if( jQuery.inArray( 'alignleft', classes_arr ) > -1 ) curr_img_class = 'alignleft';
		else if( jQuery.inArray( 'alignright', classes_arr ) > -1 ) curr_img_class = 'alignright';
		else if( jQuery.inArray( 'aligncenter', classes_arr ) > -1 ) curr_img_class = 'aligncenter';
		else curr_img_class = 'alignleft';

		// wrap with wrapper
		if( wrap_parent != '' ) { // wrap the link too
			// is there a caption?
			var wp_caption_class = '';
			var wp_caption = !wrap_parent.parent().attr( 'class' ) ? '' : wrap_parent.parent().attr( 'class' ); // get the class
			if( wp_caption.indexOf( 'wp-caption' ) != -1 ) {
				var wp_caption_array = wp_caption.split( ' ' );
				if( jQuery.inArray( 'alignleft', wp_caption_array ) > -1 ) { 
					wp_caption_class = 'alignleft';
				} else if( jQuery.inArray( 'alignright', wp_caption_array ) > -1 ) { 
					wp_caption_class = 'alignright';
				} else if( jQuery.inArray( 'aligncenter', wp_caption_array ) > -1 ) { 
					wp_caption_class = 'aligncenter';
				}
			}
			
			wrap_parent.wrap( wrap_with );
			if( wp_caption_class == '' ) wrap_parent.parent().addClass( curr_img_class );
			else {
				var mine_width = parseInt( wrap_me.width() ) + 2;
				wrap_parent.parent().removeClass( wp_caption_class );
				wrap_parent.parent().parent().css( 'width', mine_width );
			}
			wrap_me.after( append_shadow );
			// rel attribute?
			var rel_value = !wrap_parent.attr( 'rel' ) ? '' : wrap_parent.attr( 'rel' );
			if( rel_value != '' ) {
				var arr_rels = rel_value.split( '|' ); // for example limited|limited edition : [ 0 ] = class, [ 1 ] = string/tag
				if( rel_value.indexOf( 'attachment' ) == -1 ) {
					wrap_parent.before( add_rel_before );
					var rel_elem = wrap_parent.parent().find( 'span.prodtag' );
					jQuery( rel_elem ).text( arr_rels[ 1 ] );
					jQuery( rel_elem ).addClass( arr_rels[ 0 ] );
				}
			}
		} else { // no link...wrap up img only
			var parent_parent_class = !wrap_me.parent().attr( 'class' ) ? '' : wrap_me.parent().attr( 'class' );
			var my_width = parseInt( wrap_me.width() ) + 2;
			wrap_me.wrap( wrap_with );
			wrap_me.parent().addClass( curr_img_class );
			if( parent_parent_class.indexOf( 'wp-caption' ) != -1 ) wrap_me.parent().parent().css( 'width', my_width );
			wrap_me.after( append_shadow );
		}
		
	} );
}